package cn.shyouchi.wuguanjia.android.ui.home.security.thread.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.base.BaseFragment;
import cn.shyouchi.wuguanjia.android.model.BannerModule;
import cn.shyouchi.wuguanjia.android.model.NewsModel;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import io.realm.Realm;
import io.realm.RealmResults;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment implements BGABanner.OnItemClickListener {
    @BindView(R.id.topBar)
    TopBar topBar;
    @BindView(R.id.rv_module)
    RecyclerView rvModule;
    @BindView(R.id.banner)
    BGABanner mBanner;
    @BindView(R.id.news_board)
    BGABanner mNews;
    ModuleAdapter mAdapter;
    List<HomeModule> modules = new ArrayList<>();

    boolean isModuleChange = false;


    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public void initInjector() {

    }

    @Override
    public int initContentView() {
        return R.layout.fragment_main;
    }

    @Override
    public void getBundle(Bundle bundle) {


        if (modules.isEmpty() || isModuleChange) {
            reloadModule();
        }

    }

    @Override
    public void initUI(View view) {
        ButterKnife.bind(this, view);
        topBar.setLeftIcon(R.mipmap.ic_home_topbar_scan);
        topBar.setTitle("苏宁天御");
        ((BaseActivity) getActivity()).setSupportActionBar(topBar.getToolbar());
        mBanner.setOnItemClickListener(this);
        rvModule.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mAdapter = new ModuleAdapter(modules);
        rvModule.setAdapter(mAdapter);

        initBanner();
        initNews();

        mAdapter.notifyDataSetChanged();
        showContent(false);
    }

    @Override
    public void initData() {

    }


    private void initBanner() {
        List<BannerModule> modules = new ArrayList<>();
        List<String> tips = new ArrayList<>();
        modules.add(new BannerModule(R.mipmap.home_banner_default_bg6));
        tips.add("Banner 06");
        modules.add(new BannerModule(R.mipmap.home_banner_default_bg1));
        tips.add("Banner 01");


        modules.add(new BannerModule(R.mipmap.home_banner_default_bg2));
        tips.add("Banner 02");
        modules.add(new BannerModule(R.mipmap.home_banner_default_bg3));
        tips.add("Banner 03");

        mBanner.setAdapter(new BannerAdapter(getContext()));
        mBanner.setData(modules, tips);



    }


    private void initNews() {
        List<NewsModel> newsModels = new ArrayList<>();
        List<String> tips = new ArrayList<>();
        newsModels.add(new NewsModel(R.mipmap.home_banner_default_bg4));
        tips.add("这个是新闻的标题标题标题标题标题 04");
        newsModels.add(new NewsModel(R.mipmap.home_banner_default_bg5));
        tips.add("这个是新闻的标题标题标题标题标题 05");
        mNews.setAdapter(new NewsAdapter(getContext()));
        mNews.setData(newsModels, tips);
    }


    @Override
    public void onBannerItemClick(BGABanner banner, View view, Object model, int position) {

    }


    private void reloadModule() {
        modules.clear();
        Realm realm = Realm.getDefaultInstance();
        RealmResults<HomeModule> results = realm.where(HomeModule.class)
                .equalTo("show", true)
                .findAllSorted("index");
        for (HomeModule m : results) {
            if (m.isShow()) {
                modules.add(m);
            }
        }
        isModuleChange = false;
    }


    public void needRefresh() {
        isModuleChange = true;
    }


}
